package com.tiantian.quanminxiu.presenter;

import com.shenglian.utils.utils.CommonUtils;
import com.tiantian.quanminxiu.api.AnchorService;
import com.tiantian.quanminxiu.utils.ApplicationBase;
import com.tiantian.quanminxiu.utils.Constants;
import com.tiantian.quanminxiu.utils.UserData;
import com.tiantian.quanminxiu.view.MainHallView;

import rx.android.schedulers.AndroidSchedulers;

/**
 * Created by admin on 2016/8/11.
 */
public class MainHallPresent implements Presenter<MainHallView> {

    private MainHallView mainHallView;

    @Override
    public void attachView(MainHallView view) {
        this.mainHallView = view;
    }

    @Override
    public void detachView() {
        mainHallView = null;
    }


    public void initHall(int total,int iDisplayStart,int iDisplayLength){
        mainHallView.Loading_Open();
        ApplicationBase app = ApplicationBase.get(mainHallView.getContext());
        AnchorService service = app.getAnchorService();
        service.user_useranchor_page(UserData.Current.mid,UserData.Current.sign,Constants.APP_TYPE,"user.user-anchor.page",
                true,true,total,0,iDisplayStart,iDisplayLength,"Heat")
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(app.defaultSubscribeScheduler())
                .finallyDo(()->{
                    mainHallView.Loading_Close();
                })
                .subscribe(str -> {
                    int P_Total = str.optInt("iTotalRecords");
                    mainHallView.getData(str.optJSONArray("aaData"),iDisplayStart+iDisplayLength,P_Total);
                }, throwable -> {
                    CommonUtils.showToask(mainHallView.getContext(), throwable.getMessage());
                });

    }
    public void initBanner(){
        mainHallView.Loading_Open();
        ApplicationBase app = ApplicationBase.get(mainHallView.getContext());
        AnchorService service = app.getAnchorService();
        service.banner(UserData.Current.mid, UserData.Current.sign, Constants.APP_TYPE)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(app.defaultSubscribeScheduler())
                .finallyDo(()->{
                    mainHallView.Loading_Close();
                })
                .subscribe(str -> {
                    mainHallView.initBanner(str.optJSONArray("data"));
                }, throwable -> {
                    CommonUtils.showToask(mainHallView.getContext(), throwable.getMessage());
                });

    }

}
